package cn.itcast.service;

import cn.itcast.dao.UserDao;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author: cuisensen
 * data: 2018/6/18.
 * time: 17:18.
 */
public class UserServiceImp implements UserService {
    private UserDao ud;
//    private TransactionTemplate tt;
//    @Override
//    public void transfer(Integer from, Integer to, Double money) {
//        tt.execute(new TransactionCallbackWithoutResult() {
//            @Override
//            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
//                ud.decreaseMoney(from,money);
//                int x=1/0;
//                ud.increaseMoney(to,money);
//            }
//        });
//
//    }
//    public void setTt(TransactionTemplate tt) {
//        this.tt = tt;
//    }
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,readOnly = false)
    public void transfer(Integer from, Integer to, Double money) {
        ud.decreaseMoney(from,money);
        //int x=1/0;
        ud.increaseMoney(to,money);
    }
    public UserDao getUd() {
        return ud;
    }

    public void setUd(UserDao ud) {
        this.ud = ud;
    }


}
